package org.chandler.ccgp.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.chandler.ccgp.data.Task;
import org.chandler.util.SqlUtil;

public class TaskDAO {
	
	/**
	 * 初始化任务时插入
	 * @param t
	 * @return
	 */
	public static int insertInit(Task t){
		int id = 0;
		Connection conn = null;
		try {
			conn = SqlUtil.GetConn();
			Statement stmt = conn.createStatement();
			stmt.execute(genInsertInitSql(t), Statement.RETURN_GENERATED_KEYS);
			ResultSet rs = stmt.getGeneratedKeys();  
			if(rs.next()){  
			    id = rs.getInt(1);  
			}
		} catch (SQLException e) {
			SqlUtil.CloseConn(conn);
			e.printStackTrace();
		}
		SqlUtil.CloseConn(conn);
		return id;
	}
	
	private static String genInsertInitSql(Task t){
		StringBuffer sb = new StringBuffer();
		sb.append("INSERT INTO `task`(`url`, `sdate`, `edate`, `totalPage`, `totalNum`) VALUES");
		sb.append("('");
		sb.append(t.url + "','");
		sb.append(t.sDate + "','");
		sb.append(t.eDate + "',");
		sb.append(t.totalPage + ",");
		sb.append(t.totalNum + ")");
		return sb.toString();
	}
}
